package org.genitus.druidry

import com.fasterxml.jackson.databind.{JavaType, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule

object JacksonUtil {
  private[this] val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule)

  def toJson[T](t: T): String = {
    objectMapper.writeValueAsString(t)
  }

  def parseArray[T](json: String, javaType: JavaType): List[T] = {
    val _javaType: JavaType = objectMapper.getTypeFactory.constructParametricType(classOf[List[_]], javaType)
    objectMapper.readValue[List[T]](json, _javaType)
  }

  def javaType(pclazz: Class[_],cclazz: Class[_]):JavaType= {
    objectMapper.getTypeFactory.constructParametricType(pclazz, cclazz)
  }
}
